package com.gxj.study.shopdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gxj.study.shopdemo.R;
import com.gxj.study.shopdemo.bean.Campaign;
import com.gxj.study.shopdemo.bean.HomeCampaign;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 祥杰 on 2016/8/3.
 */

public class HomeCatgoryAdapter extends RecyclerView.Adapter<HomeCatgoryAdapter.ViewHolder> {

    private static int VIEW_TYPE_L = 0;
    private static int VIEW_TYPE_R = 1;
    private LayoutInflater mInflater;
    private List<HomeCampaign> mDatas;
    private Context mContext;
    private OnCampaignClickListener mListener;

    public HomeCatgoryAdapter(List<HomeCampaign> datas, Context context) {

        mDatas = datas;
        this.mContext = context;
    }

    public void setOnCampaignClickListener(OnCampaignClickListener onCampaignClickListener) {
        this.mListener = onCampaignClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
        mInflater = LayoutInflater.from(viewGroup.getContext());
        if (type == VIEW_TYPE_R) {
            return new ViewHolder(mInflater.inflate(R.layout.template_home_cardview2, viewGroup, false));
        }
        return new ViewHolder(mInflater.inflate(R.layout.template_home_cardview, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        HomeCampaign compaign = mDatas.get(position);
        holder.textTitle.setText(compaign.getTitle());
        Picasso.with(mContext).load(compaign.getCpOne().getImageUrl()).into(holder.imageViewBig);
        Log.d("url",compaign.getCpOne().getImageUrl());
        Picasso.with(mContext).load(compaign.getCpTwo().getImageUrl()).into(holder.imageViewSmallTop);
        Picasso.with(mContext).load(compaign.getCpThree().getImageUrl()).into(holder.imageViewSmallBootom);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return VIEW_TYPE_R;
        } else {
            return VIEW_TYPE_L;
        }
    }

     class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView textTitle;
        ImageView imageViewBig;
        ImageView imageViewSmallTop;
        ImageView imageViewSmallBootom;

        public ViewHolder(View itemView) {
            super(itemView);
            textTitle = (TextView) itemView.findViewById(R.id.text_title);
            imageViewBig = (ImageView) itemView.findViewById(R.id.imgVIew_big);
            imageViewSmallTop = (ImageView) itemView.findViewById(R.id.imageView_small_top);
            imageViewSmallBootom = (ImageView) itemView.findViewById(R.id.imageView_small_bottom);

            imageViewBig.setOnClickListener(this);
            imageViewSmallTop.setOnClickListener(this);
            imageViewSmallBootom.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            HomeCampaign homeCampaign = mDatas.get(getLayoutPosition());
            if(mListener != null){
               switch (view.getId()){
                   case R.id.imgVIew_big:
                       mListener.onClick(view,homeCampaign.getCpOne());
                       break;
                   case R.id.imageView_small_top:
                       mListener.onClick(view,homeCampaign.getCpTwo());
                       break;
                   case R.id.imageView_small_bottom:
                       mListener.onClick(view,homeCampaign.getCpThree());
                       break;
               }
            }
        }
    }

    public interface OnCampaignClickListener {

        void onClick(View view, Campaign campaign);

    }
}
